<?php
namespace Admin\Controller;

class StatisticController extends BaseController{
	public function index(){
		$Order = D('Order');
		
		$data = array();
		//今天
		$data['day1']['time'] = date('Y-m-d');
		$data['day1']['data'] = $this->diyTime($Order->diyTime($data['day1']['time'],$data['day1']['time']));
		//昨天
		$data['day2']['time'] = date('Y-m-d',strtotime('-1 day'));
		$data['day2']['data'] = $this->diyTime($Order->diyTime($data['day2']['time'],$data['day2']['time']));
		//本周
		$data['week1']['time1'] = date('Y-m-d',time()-((date('w'))*86400));
		$data['week1']['time2'] = date('Y-m-d',time()+((6-date('w'))*86400));
		$data['week1']['data'] = $this->diyTime($Order->diyTime($data['week1']['time1'],$data['week1']['time2']));
		//最近一周
		$data['week2']['time1'] = date('Y-m-d',time()-518400);
		$data['week2']['time2'] = date('Y-m-d');
		$data['week2']['data'] = $this->diyTime($Order->diyTime($data['week2']['time1'],$data['week2']['time2']));
		//本月
		$data['month1']['time1'] = date('Y-m').'-01';
		$data['month1']['time2'] = date('Y-m-t');
		$data['month1']['data'] = $this->diyTime($Order->diyTime($data['month1']['time1'],$data['month1']['time2']));
		//最近一月
		$data['month2']['time1'] = date('Y-m-d',time()-2592000);
		$data['month2']['time2'] = date('Y-m-d');
		$data['month2']['data'] = $this->diyTime($Order->diyTime($data['month2']['time1'],$data['month2']['time2']));
		//今年
		$data['year1']['time1'] = date('Y').'-01-01';
		$data['year1']['time2'] = date('Y').'-12-31';
		$data['year1']['data'] = $this->diyTime($Order->diyTime($data['year1']['time1'],$data['year1']['time2']));
		//最近一年
		$data['year2']['time1'] = date('Y-m-d',time()-31449600);
		$data['year2']['time2'] = date('Y-m-d');
		$data['year2']['data'] = $this->diyTime($Order->diyTime($data['year2']['time1'],$data['year2']['time2']));
		//总计
		$old = $Order->older();
		$new = $Order->newer();
		$data['add']['time1'] = date('Y-m-d',$old['date']);
		$data['add']['time2'] = date('Y-m-d',$new['date']);
		$data['add']['data'] = $this->diyTime($Order->diyTime($data['add']['time1'],$data['add']['time2']));
		$this->assign('Data',$data);

		$OrderController = new OrderController();
		$OrderController->field(I('get.field'));
		$OrderController->admin(I('get.uid',-1));
		$OrderController->product(I('get.pid'));
		$OrderController->logistics(I('get.lid'));
		$OrderController->template(I('get.tid'));
		$OrderController->ostate(I('get.state'));
		$this->assign('Pay',$OrderController->pay);
		$this->display();
	}
	
	public function day(){
		$this->dayMonthYear('%Y年%m月%d日');
	}
	
	public function month(){
		$this->dayMonthYear('%Y年%m月');
	}
	
	public function year(){
		$this->dayMonthYear('%Y年');
	}
	
	private function diyTime($object){
		if ($object){
			$data = array('count1'=>0,'count2'=>0,'count3'=>0,'count4'=>0,'count5'=>0,'sum1'=>0,'sum2'=>0,'sum3'=>0,'sum4'=>0,'sum5'=>0);
			foreach ($object as $value){
				$data['count1'] += $value['count1'];
				$data['count2'] += $value['count2'];
				$data['count3'] += $value['count3'];
				$data['sum1'] += $value['sum1'];
				$data['sum2'] += $value['sum2'];
				$data['sum3'] += $value['sum3'];
				$data['sum1'] = number_format($data['sum1'],2,'.','');
				$data['sum2'] = number_format($data['sum2'],2,'.','');
				$data['sum3'] = number_format($data['sum3'],2,'.','');
				$data['count4'] = $data['count1'] + $data['count2'] + $data['count3'];
				$data['count5'] = $data['count1'] + $data['count2'];
				$data['sum4'] = number_format($data['sum1'] + $data['sum2'] + $data['sum3'],2,'.','');
				$data['sum5'] = number_format($data['sum1'] + $data['sum2'],2,'.','');
			}
		}
		return $data;
	}
	
	private function dayMonthYear($time){
		$Order = D('Order');
		
		if (IS_POST){
			$this->checkPermit2('Statistic','output');
			$object = $Order->dayMonthYear($time);
			$output = '"时间","未发货数","已发货数","已取消数","未发货金额","已发货金额","已取消金额","订单数","成交数","订单金额","成交金额",';
			if ($object){
				$count1 = $count2 = $count3 = $count4 = $count5 = $sum1 = $sum2 = $sum3 = $sum4 = $sum5 = 0;
				foreach ($object as $value){
					$count1 += $value['count1'];
					$count2 += $value['count2'];
					$count3 += $value['count3'];
					$sum1 += $value['sum1'];
					$sum2 += $value['sum2'];
					$sum3 += $value['sum3'];
					$count4 = $count1 + $count2 + $count3;
					$count5 = $count1 + $count2;
					$sum4 = $sum1 + $sum2 + $sum3;
					$sum5 = $sum1 + $sum2;
					$output .= "\r\n".'"'.$value['time'].'","'.$count1.'","'.$count2.'","'.$count3.'","'.$sum1.'","'.$sum2.'","'.$sum3.'","'.$count4.'","'.$count5.'","'.$sum4.'","'.$sum5.'",';
					$count1 = $count2 = $count3 = $count4 = $count5 = $sum1 = $sum2 = $sum3 = $sum4 = $sum5 = 0;
				}
			}
			$output = mb_convert_encoding($output,'GBK','UTF-8');
			downFile($output,mb_convert_encoding('订单统计报表_'.time().'.csv','GBK','UTF-8'));
			exit;
		}
		
		$object = $Order->dayMonthYear($time);
		$data = array('count1'=>0,'count2'=>0,'count3'=>0,'count4'=>0,'count5'=>0,'sum1'=>0,'sum2'=>0,'sum3'=>0,'sum4'=>0,'sum5'=>0);
		if ($object){
			foreach ($object as $value){
				$data['count1'] += $value['count1'];
				$data['count2'] += $value['count2'];
				$data['count3'] += $value['count3'];
				$data['sum1'] += $value['sum1'];
				$data['sum2'] += $value['sum2'];
				$data['sum3'] += $value['sum3'];
				$data['sum1'] = number_format($data['sum1'],2,'.','');
				$data['sum2'] = number_format($data['sum2'],2,'.','');
				$data['sum3'] = number_format($data['sum3'],2,'.','');
				$data['count4'] = $data['count1'] + $data['count2'] + $data['count3'];
				$data['count5'] = $data['count1'] + $data['count2'];
				$data['sum4'] = number_format($data['sum1'] + $data['sum2'] + $data['sum3'],2,'.','');
				$data['sum5'] = number_format($data['sum1'] + $data['sum2'],2,'.','');
			}
		}
		$this->assign('Data',$data);
		
		$object = $Order->dayMonthYear($time,$this->page(count($Order->dayMonthYear($time))));
		if ($object){
			foreach ($object as $key=>$value){
				$object[$key]['count4'] = $value['count1'] + $value['count2'] + $value['count3'];
				$object[$key]['count5'] = $value['count1'] + $value['count2'];
				$object[$key]['sum4'] = number_format($value['sum1'] + $value['sum2'] + $value['sum3'],2,'.','');
				$object[$key]['sum5'] = number_format($value['sum1'] + $value['sum2'],2,'.','');
			}
		}
		$this->assign('All',$object);
		
		$param = '?c='.CONTROLLER_NAME.'&a='.ACTION_NAME;
		foreach (I('get.') as $key=>$value){
			if (!in_array($key,array('m','c','a','page','order'))) $param .= '&'.$key.'='.$value;
		}
		$this->assign('Param',$param);
		
		$OrderController = new OrderController();
		$OrderController->field(I('get.field'));
		$OrderController->admin(I('get.uid',-1));
		$OrderController->product(I('get.pid'));
		$OrderController->logistics(I('get.lid'));
		$OrderController->template(I('get.tid'));
		$OrderController->ostate(I('get.state'));
		$this->assign('Pay',$OrderController->pay);
		$this->display();
	}
}